import scrapy
from demo2.items import zonghengItem
# scrapy crawl zongheng -o zongheng.csv

class ZonghongSpider(scrapy.Spider):
    name = "zongheng"
    allowed_domains = ["zongheng.com"]
    start_urls = ["https://www.zongheng.com/api/rank/details"]

    def start_requests(self):
        # 定义 POST 参数
        params = {
            'cateFineId': '0',
            'cateType': '0',
            'pageNum': '1',
            'pageSize': '20',
            'period': '0',
            'rankNo': '20253',
            'rankType': '1'
        }

        user_agent = self.settings.get('USER_AGENT')
        headers = {
            "Referer": "https://www.zongheng.com/rank?nav=monthly-ticket&rankType=1&month=20253",
            "User-Agent": user_agent
        }

        yield scrapy.FormRequest(
            url=self.start_urls[0],
            formdata=params,
            headers=headers
        )

    def parse(self, response):
        json_data = response.json()
        resultList = json_data['result']['resultList']
        for result in resultList:
            item = zonghengItem()

            # item['author_id'] = result.get('authorId')
            # item['category_id'] = result.get('cateFineId')
            item['category_name'] = result.get('cateFineName')
            # item['book_id'] = result.get('bookId')
            item['book_name'] = result.get('bookName')
            # item['book_cover'] = result.get('bookCover')
            item['author_name'] = result.get('pseudonym')
            # item['serial_status'] = result.get('serialStatus')
            item['yuepiao_num'] = result.get('number')

            # 处理特殊字段
            item['description'] = result.get('description').replace('\r\n', ' ').strip()
            item['latest_chapter_time'] = result.get('latestChapterTime')
            # item['latest_chapter_time'] = f"2025-{json_data.get('latestChapterTime')}"  # 补全年份

            # 处理章节名称
            chapter_name = result.get('latestChapterName')
            item['latest_chapter_name'] = chapter_name.lstrip('0123456789、.') if chapter_name else ''
            # item['latest_chapter_name'] =json_data.get('latestChapterName')

            yield item

        pass
